﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TTSoft.MyApp.UserManagers
{
    #region Using
    using TTSoft.MyApp.Helpers;
    using TTSoft.MyApp.Repositories;
    using DevComponents.DotNetBar;
    using System.Windows.Forms;
    #endregion

    public class GlobalUserManagers
    {
        /// <summary>
        /// Retrieve name from enum function
        /// </summary>
        /// <param name="chucnang"></param>
        /// <returns></returns>
        public static string RetrieveTenChucNang(EnumChucNangHeThong chucnang)
        {
            var tenchucnang = "";
            switch (chucnang)
            {
                // NGUOI DUNG
                case EnumChucNangHeThong.QUANTRINGUOIDUNG:
                    tenchucnang = "Quản trị người dùng";
                    break;
                case EnumChucNangHeThong.THAYDOIMATKHAU:
                    tenchucnang = "Thay đổi mật khẩu";
                    break;
            }
            return tenchucnang;
        }

        /// <summary>
        /// Retrieve enum function from name
        /// </summary>
        /// <param name="chucnang"></param>
        /// <returns></returns>
        public static EnumChucNangHeThong RetrieveFuncsNotAllowUsing(string tenchucnang)
        {
            EnumChucNangHeThong func = EnumChucNangHeThong.QUANTRINGUOIDUNG;
            if (tenchucnang == "Quản trị người dùng")
            {
                func = EnumChucNangHeThong.QUANTRINGUOIDUNG;
            }
            else if (tenchucnang == "Thay đổi mật khẩu")
            {
                func = EnumChucNangHeThong.THAYDOIMATKHAU;
            }
            return func;
        }

        /// <summary>
        /// Mapping name of controls with specifed enums so that enable or disable some functions for each users
        /// </summary>
        /// <returns></returns>
 
        public static Dictionary<string, EnumChucNangHeThong> InitFuncsMapping()
        {
            var dict = new Dictionary<string, EnumChucNangHeThong>();

            dict.Add("menQuanLyLoaiNguoiDung", EnumChucNangHeThong.QUANTRINGUOIDUNG);
            dict.Add("menQuanLyNguoiDung", EnumChucNangHeThong.QUANTRINGUOIDUNG);
            dict.Add("menQuanLyChucNang", EnumChucNangHeThong.QUANTRINGUOIDUNG);
            dict.Add("menQuanLyChucNangNhomNguoiDung", EnumChucNangHeThong.QUANTRINGUOIDUNG);
            dict.Add("menDoiMatKhau", EnumChucNangHeThong.THAYDOIMATKHAU);

            return dict;
        }

        public static List<EnumChucNangHeThong> InitFuncsAllowUsing()
        {
            var lstAllFuncs = ChucNangRepository.SelectAll();
            var lstFuncsBelongToUser = LoaiNguoiDung_ChucNangRepository.SelectByIDLoaiNguoiDung(GlobalVars.g_CurrentUser.LoaiNguoiDung.IDLoaiNguoiDung);
            var lst = new List<EnumChucNangHeThong>();
            foreach (var item in lstAllFuncs)
            {
                foreach (var item2 in lstFuncsBelongToUser)
                {
                    if (item.IDChucNang == item2.IDChucNang)
                    {
                        lst.Add(RetrieveFuncsNotAllowUsing(item.TenChucNang));
                        break;
                    }
                }
            }
            return lst;
        }

        private static void DisableCtrls(Panel pnlMain, string shortcut)
        {
            foreach (Control objControl in pnlMain.Controls)
            {
                if (objControl.GetType() == typeof(MenuStrip))
                {
                    foreach (ToolStripMenuItem _item in ((MenuStrip)objControl).Items)
                    {
                        DiableMenuItem(_item, shortcut);
                    }
                }
                else if (objControl.GetType() == typeof(ToolStrip))
                {
                    foreach (var _item in ((ToolStrip)objControl).Items)
                    {
                        if (_item.GetType() == typeof(ToolStripButton))
                        {
                            DiableToolStripItem((ToolStripButton)_item, shortcut);
                        }
                    }
                }
                else if (objControl.GetType() == typeof(ExplorerBar))
                {
                    foreach (ExplorerBarGroupItem _item in ((ExplorerBar)objControl).Groups)
                    {
                        DiableButtonItem(_item, shortcut);
                    }
                }
            }
        }

        private static void DiableButtonItem(ExplorerBarGroupItem parentItem, string shortcut)
        {
            if (parentItem != null && parentItem.SubItems.Count > 0)
            {
                foreach (var _item in parentItem.SubItems)
                {
                    if (_item.GetType() == typeof(ButtonItem))
                    {
                        if (shortcut == ((ButtonItem)_item).Name)
                        {
                            ((ButtonItem)_item).Enabled = true;
                            return;
                        }
                    }
                }
            }
        }

        private static void DiableToolStripItem(ToolStripButton parentItem, string shortcut)
        {
            if (parentItem.Name == shortcut)
            {
                parentItem.Enabled = true;
            }
        }

        private static void DiableMenuItem(ToolStripMenuItem parentItem, string shortcut)
        {
            if (parentItem != null)
            {
                foreach (var _item in parentItem.DropDownItems)
                {
                    if (_item.GetType() == typeof(ToolStripMenuItem))
                    {
                        DiableMenuItem((ToolStripMenuItem)_item, shortcut);
                    }
                }
                if (parentItem.Name == shortcut)
                {
                    parentItem.Enabled = true;
                }
            }
        }

        /// <summary>
        /// Disable some functions that user not allow using
        /// </summary>
        public static void EnableFuncAllowUsing(Panel pnlMain, string ctrlName)
        {
            DisableCtrls(pnlMain, ctrlName);
        }
    }
}
